package com.woniuxy01;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;


interface MyInterface<T>{
    boolean test(T t);
}

public class Test03 {
    public static void main(String[] args) {
        List<Account> accounts = getList();
        //调用forEach()方法，参数是接口
        // 我们使用匿名内部类，实现将对象输出
//        accounts.forEach(new Consumer<Account>() {
//            @Override
//            public void accept(Account account) {
//                System.out.println(account);
//            }
//        });
        accounts.forEach(account ->System.out.println(account) );
        //方法引用
        accounts.forEach(System.out::println);
    }

    private static void test03() {
        List<Account> accounts = getList();
//        System.out.println(filter01(accounts));
//        System.out.println(filter02(accounts));


//        List<Account> list = filter(accounts, new MyInterface<Account>() {
//            @Override
//            public boolean test(Account account) {
//                return account.getAname().startsWith("刘");
//            }
//        });
        List<Account> list = filter(accounts, account->  account.getAname().startsWith("刘"));
        System.out.println(list);
        List<Account> list2 = filter(accounts, account->  account.getSex().equals("女"));
        System.out.println(list2);
        List<Account> list3 = filter(accounts, account->  account.getAname().length()==3);
        System.out.println(list3);
    }

    static List<Account> filter(List<Account> accounts,MyInterface<Account> myInterface){
        List<Account> list=new ArrayList<>();
        for(Account account: accounts){
            if(myInterface.test(account)){
                list.add(account);
            }
        }
        return list;
    }

    //筛选性别为男的账号
    static List<Account> filter02(List<Account> accounts){
        List<Account> list=new ArrayList<>();
        for(Account account: accounts){
            if(account.getSex().equals("男")){
                list.add(account);
            }
        }
        return list;
    }

    //筛选姓刘的账号
    static List<Account> filter01(List<Account> accounts){
        List<Account> list=new ArrayList<>();
        for(Account account: accounts){
            if(account.getAname().startsWith("刘")){
                list.add(account);
            }
        }
        return list;
    }



    private static void test02() {
        List<Account> accounts = getList();
        accounts.stream()
                .filter(account -> account.getAname().startsWith("刘"))
                .toList()
                .forEach(System.out::println);
    }

    private static void test01() {
        List<Account> accounts = getList();
        System.out.println(accounts);
        //查询姓刘的账户信息
        List<Account> list=new ArrayList<>();
        for(Account account: accounts){
            if(account.getAname().startsWith("刘")){
                list.add(account);
            }
        }
        System.out.println(list);
    }


    static List<Account> getList() {
        Account a1 = new Account(10, "刘口水", "123", "男", 2000, "成都市", "18868036677", new Date());
        Account a2 = new Account(102, "刘言", "123", "女", 8000, "宜宾市", "18868036688", new Date());
        Account a3 = new Account(101, "刘备", "123", "男", 20000, "成都市", "18868036557", new Date());
        Account a4 = new Account(30, "杜脐眼", "123", "女", 28000, "宜宾市", "18868036337", new Date());
        Account a5 = new Account(50, "杜子谦", "123", "男", 6000, "成都市", "18868032277", new Date());
        Account a6 = new Account(20, "杜甫", "123", "男", 20000, "乐山市", "18868030077", new Date());
        Account a7 = new Account(14, "杨洋", "123", "男", 10000, "成都市", "18868036637", new Date());

        List<Account> list = new ArrayList<Account>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        list.add(a4);
        list.add(a5);
        list.add(a6);
        list.add(a7);
        return list;
    }
}
